package net.yla.smartbus.module.ba.equipment.serice.impl;

import com.github.pagehelper.PageHelper;
import net.yla.smartbus.core.base.PageBean;
import net.yla.smartbus.module.ba.equipment.detachment.DeviceDetachment;
import net.yla.smartbus.module.ba.equipment.dto.DeviceDto;
import net.yla.smartbus.module.ba.equipment.entity.BaDevicesDo;
import net.yla.smartbus.module.ba.equipment.form.DeviceForm;
import net.yla.smartbus.module.ba.equipment.mapper.DeviceMapper;
import net.yla.smartbus.module.ba.equipment.query.DeviceQuery;
import net.yla.smartbus.module.ba.equipment.serice.DeviceService;
import net.yla.smartbus.module.ba.vehicle.common.ExcelData;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.CellType;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 默苍离
 * @date 2021/6/22 10:02
 * <p>
 * ******************************************
 * @annotation:
 */
@Service
public class DeviceServiceImpl implements DeviceService {

    private final DeviceMapper mapper;
    private final DeviceDetachment deviceDetachment;

    public DeviceServiceImpl(DeviceMapper mapper, DeviceDetachment deviceDetachment) {
        this.mapper = mapper;
        this.deviceDetachment = deviceDetachment;
    }


    /**
     * 根据查询条件获取 所有 设备信息
     *
     * @param query
     * @return
     */
    @Override
    public List<DeviceDto> getList(DeviceQuery query) {
        List<BaDevicesDo> lists = mapper.getList(query);
        List<DeviceDto> devices = new ArrayList<>();
        if (lists != null) {
            for (int i = 0; i < lists.size(); i++) {
                BaDevicesDo list = lists.get(i);
                DeviceDto deviceDto = deviceDetachment.deviceDoToTto(list);
                devices.add(deviceDto);
            }

        }
        return devices;
    }


    /**
     * 分页查询
     *
     * @param query
     * @return
     */
    @Override
    public PageBean<DeviceDto> getListByPage(DeviceQuery query) {
        Integer count = mapper.getCount(query);
        PageHelper.startPage(query.getPageNum(), query.getPageSize(), "creationTime desc");
        List<DeviceDto> list = getList(query);
        PageBean<DeviceDto> pageBean = new PageBean(query.getPageNum(), query.getPageSize(), count, list);
        return pageBean;
    }


    /**
     * 导出设备
     *
     * @param query
     * @return
     */
    @Override
    public ExcelData exportDevice(DeviceQuery query) {
        ExcelData excelData = new ExcelData();
        excelData.setFileName("设备导出");
        List<String> head = new ArrayList<>();
        head.add("设备编号");
        excelData.setHead(head);
        List<BaDevicesDo> list = mapper.getList(query);

        List<List<String>> data = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            BaDevicesDo baVehiclesDo = list.get(i);
            List<String> content = new ArrayList<>();
            content.add(baVehiclesDo.getNo());
            data.add(content);
        }
        excelData.setData(data);

        return excelData;
    }


    /**
     * 导入设备
     *
     * @param file
     * @return
     */
    @Override
    public Integer insertByImport(MultipartFile file) throws IOException {

        InputStream inputStream = file.getInputStream();
        //由输入流文件得到工作簿对象
        HSSFWorkbook wb = new HSSFWorkbook(inputStream);
        //获取第一个sheet
        HSSFSheet sheet = wb.getSheetAt(0);
        //获取表格内容的最后一行的行数
        int lastRowNum = sheet.getLastRowNum();

        //rowBegin代表要开始读取的行号，下面这个循环的作用是读取每一行内容
        for (int i = 1; i <= lastRowNum; ++i) {
            DeviceForm excelForm = new DeviceForm();
            //获取每一行
            HSSFRow row = sheet.getRow(i);
            //获取每一行的最后一列的列号，即总列数
            int columnNum = row.getLastCellNum();
            for (int j = 0; j < columnNum; ++j) {
                //获取每个单元格
                HSSFCell cell = row.getCell(j);
                //设置单元格类型
                cell.setCellType(CellType.STRING);
                String cellValue = cell.getStringCellValue();
                if (j == 0) {
                    excelForm.setNo(cellValue);
                }
            }
            mapper.insert(excelForm);
        }
        wb.close();
        return 1;
    }


    /**
     * 新增设备
     *
     * @param form
     * @return
     */
    @Override
    public Integer insert(DeviceForm form) {
        return mapper.insert(form);
    }


    /**
     * 根据 设备id 查询 一台 设备信息
     *
     * @param deviceId
     * @return
     */
    @Override
    public DeviceDto getOneById(Integer deviceId) {
        BaDevicesDo oneById = mapper.getOneById(deviceId);
        DeviceDto deviceDto = deviceDetachment.deviceDoToTto(oneById);
        return deviceDto;
    }


    /**
     * 更新设备
     *
     * @param form
     * @return
     */
    @Override
    public Integer update(DeviceForm form) {
        return mapper.update(form);
    }


}
